import 'dart:convert';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/message.dart';
import 'package:lcapp/api/interface/user_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/data/message_data_entity.dart';
import 'package:lcapp/data/user_data_entity.dart';
import 'package:lcapp/model/refresh/refresh.dart';
import 'package:lcapp/model/toast/toast.dart';
import 'package:lcapp/tools/date_util.dart';
import 'package:lcapp/widget/asr_app_bar.dart';
import 'package:lcapp/widget/asr_cache_image.dart';

class FaxainNewFriendPage extends StatefulWidget {
  const FaxainNewFriendPage({Key? key}) : super(key: key);

  @override
  _FaxainNewFriendPageState createState() => _FaxainNewFriendPageState();
}

class _FaxainNewFriendPageState extends State<FaxainNewFriendPage> {
  int pageNum = 1;
  int pageSize = 10;

  List<MessageItem> messages = [];
  List<UserDataEntity> users = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getList();
  }

  getUsers()async{
    ResponseData res = await UserAPI.getMinFollows();
    if(res.code==200){
      users.clear();
      List arr = res.data??[];
      for (var element in arr) {
        UserDataEntity entity = UserDataEntity().fromJson(element);
        entity.attentionState = 1;
        users.add(entity);
      }
    }
    if(mounted){setState(() {
    });}
  }

  getList() async {
    ResponseData res = await MessageAPI.getMessageList(pageNum, pageSize,3);
    if (res.code == 200) {
      if(pageNum==1){
        messages.clear();
      }
      List arr = res.data??[];
      for (var element in arr) {
        MessageItem item = MessageItem().fromJson(element);
        messages.add(item);
      }
      getUsers();
    }
    if(mounted){setState(() {
    });}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AsrAppBar(
        titleText: '新朋友',
      ),
      body: Container(
        child: EasyRefresh(
          header: myRefreshHeader(),
          footer: myRefreshFooter(),
          onRefresh: ()async{
            pageNum=1;
            getList();
          },
          onLoad: ()async{
            pageNum++;
            getList();
          },
          child: ListView.builder(
              itemCount: messages.length,
              itemBuilder: (context, index) {
                MessageItem item = messages[index];
                Map<String,dynamic> exta = jsonDecode(item.exta!);
                bool isFllow = false;
                for (var element in users) {
                  if(element.id == item.fromUserId){
                    isFllow = true;
                  }
                }
                return GestureDetector(
                  onTap: (){
                    Navigator.of(context).pushNamed("ShopHomePage",arguments: item.fromUserId).then((value){
                      getUsers();
                    });
                  },
                  child: Container(
                    color: Colors.transparent,
                    height: 140.w,
                    padding: EdgeInsets.symmetric(horizontal: 30.w),
                    margin: EdgeInsets.only(bottom: 40.w),
                    child: Column(
                      children: [
                        Expanded(
                            child: Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                ClipRRect(
                                  child: ASRCacheImage(
                                    width: 90.w,
                                    height: 90.w,
                                    fit: BoxFit.fitWidth,
                                    image: exta['headImg']??'assets/icon/p_head.png',
                                  ),
                                  borderRadius: BorderRadius.circular(90.w),
                                ),
                                SizedBox(
                                  width: 30.w,
                                ),
                                Expanded(
                                    child: Column(
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Text(
                                          '${item.title??''}',
                                          style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 32.sp,
                                              fontWeight: FontWeight.w600),
                                        ),
                                        SizedBox(
                                          height: 10.w,
                                        ),
                                        Text('关注了您  ${DateUtil.getDateScope(checkDate: item.createTime)}',
                                            style: TextStyle(
                                              color: Color(0xff848484),
                                              fontSize: 28.sp,
                                            ))
                                      ],
                                    )),
                                GestureDetector(
                                  onTap: ()async{
                                    showLoading();
                                    ResponseData res = await UserAPI.followUser(item.fromUserId!);
                                    hideLoading();
                                    if(res.code==200){
                                      getUsers();
                                    }
                                  },
                                  child: Container(
                                    width: 130.w,
                                    height: 66.w,
                                    alignment: Alignment.center,
                                    decoration: BoxDecoration(
                                        color: isFllow==false?Color(0xffFF7A3D):Color(0xffFF7A3D),
                                        borderRadius: BorderRadius.circular(66.w)),
                                    child: Text(isFllow==false? '回关':'取消关注',
                                        style: TextStyle(
                                          color: Colors.white,
                                          fontSize: 24.sp,
                                        )),
                                  ),
                                ),
                              ],
                            )),
                        Container(
                          height: 1,
                          // margin: EdgeInsets.only(left: 120.w),
                          color: Color(0xffE9E8E9),
                        ),
                      ],
                    ),
                  ),
                );
              }),
        ),
      ),
    );
  }
}
